package com.gexiaobao.pigeon.ui.fragment.mine.tools.activity

import android.os.Bundle
import android.view.KeyEvent
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityRaceProofBinding
import com.gexiaobao.pigeon.ui.adapter.RaceProofAdapter
import com.gexiaobao.pigeon.viewmodel.ProofViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity


/**
 * @Author      : hxw
 * @Date        : 2024/3/21 8:47
 * @Describe    : 交费收据
 */
class ActivityRaceProof : BaseActivity<ProofViewModel, ActivityRaceProofBinding>() {

    private lateinit var loadsir: LoadService<Any>
    private val mAdapter: RaceProofAdapter by lazy { RaceProofAdapter(arrayListOf()) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        StatusBarUtil.setLightMode(this)
    }

    override fun initView(savedInstanceState: Bundle?) {
        onRefresh(true)

        /** 初始化adapter */
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 12f)))
            it.initFooter {
                /** 触发加载更多时请求数据 */
                mViewModel.getRaceVoucher(false, mDatabind.editQuery.text.toString().trim())
            }
        }

        /** 状态页配置 */
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            onRefresh(true)
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            onRefresh(true)
        }

        mAdapter.addChildClickViewIds(
            R.id.llItemProofLandCertificate,
            R.id.llItemProofCertificate,
            R.id.llItemProofLandRegulation
        )
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.llItemProofLandCertificate -> {
                    startActivity<ActivityRaceProofFile>("title" to "土地证明", "fileUrl" to mAdapter.data[position].landCertificate)
                }

                R.id.llItemProofCertificate -> {
                    startActivity<ActivityRaceProofFile>("title" to "证书", "fileUrl" to mAdapter.data[position].raceGuarantee)
                }

                R.id.llItemProofLandRegulation -> {
                    startActivity<ActivityRaceProofFile>("title" to "规程", "fileUrl" to mAdapter.data[position].raceRules)
                }
            }
        }

        mAdapter.setOnProofClickListener(object : RaceProofAdapter.OnProofClickListener {
            override fun onItemClick(positionParent: Int, positionChild: Int, path: String, name: String) {
                startActivity<ActivityRelatedDocument>("title" to name, "path" to path)
            }
        })

        mDatabind.editQuery.setOnKeyListener { _, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN) {
                onRefresh(true)
                hideSoftKeyboard(this)
            }
            false
        }
    }

    private fun onRefresh(isRefresh: Boolean) {
        showLoading(getString(R.string.loading))
        mViewModel.getRaceVoucher(isRefresh, mDatabind.editQuery.text.toString().trim())
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.ivBack, mDatabind.tvSearchResult) {
            when (it) {
                mDatabind.ivBack -> {
                    finish()
                }

                mDatabind.tvSearchResult -> {
                    onRefresh(true)
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.raceVoucherResult.observe(this) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }
}